new Vue({
	el: '#app',
	data: function () {
		return {
			loading:false,//进去的加载中显示
			host: window.location.protocol+'//'+window.location.host,
            communityId:'',
            alertList:[
                // {title:'养老设备预警',name:'门磁',mobile:'15125145252',room:'2-12-1202',time:'2021-07-14 15:25:20',type:'黄色'},
                // {title:'养老设备预警',name:'紧急按钮',mobile:'15125145252',room:'2-12-1202',time:'2021-07-15 15:25:20',type:'红色'}
            ],
            alertListResult:[
                // {title:'养老设备预警',name:'门磁',mobile:'15125145252',room:'2-12-1202',time:'2021-07-14 15:25:20',type:'黄色'},
                // {title:'养老设备预警',name:'紧急按钮',mobile:'15125145252',room:'2-12-1202',time:'2021-07-15 15:25:20',type:'红色'}
            ],
            showPopup:false,
            remark:'',
            list_length:0,
            finished_text:'没有更多了',
            loading: false,
            finished: false,
            refreshing: false,
            totalPage:1,
            pageIndex:0,
            list_length_ed:0,
            finished_text_ed:'没有更多了',
            loading_ed: false,
            finished_ed: false,
            refreshing_ed: false,
            totalPage_ed:1,
            pageIndex_ed:0,
            alertId:'',
            alertState:'',
            bstap:true,
            showToast:false,
            tip_msg:'预警规则：门磁设备，黄色或者红色预警天数内（预警天数根据老人的生活作息而定，每个老人都不一样）没有开启过门磁，则进行对应级别的短信给预警提醒。紧急按钮，黄色预警小时内按一次则短信给黄色预警提醒人员，红色预警小时内累计则按3次以上则短信给红色预警提醒人员',
            requestHeaders:'',
		}
	},
	created() {
        var that=this;
        var localStorageCommunityId=JSON.parse(localStorage.getItem('hc_currentCommunityInfo'))
		if(localStorageCommunityId){
			this.communityId=localStorageCommunityId.communityId?localStorageCommunityId.communityId:this.getUrlParam("communityId");
		}else{
			this.communityId=this.getUrlParam("communityId");
        }   
        that.requestHeaders=getRequestHeaders('05',this.communityId);//获取请求头
        // console.log(that.requestHeaders)
    },

	filters: {
		subStringThree: function (value) {
			if (!value) return ''
			return value.substring(0,3)
		}
	},
	methods: {
		getUrlParam(name) {
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
			var r = window.location.search.substr(1).match(reg); //匹配目标参数
			if (r != null) return unescape(r[2]);
			return null; //返回参数值	  
		},
        toastClick(){
            var that=this;
            this.showToast=true;
            var time='10000'
            setTimeout(function(){
                that.showToast=false;
            },time)
        },
        //tab切换
        onTabClick(tab){
            if(tab=='0'){
                this.loadList() // 加载数据
            }else{
                this.loadListProcessed() // 加载数据
            }
        },
        //去处理
        handleClick(id,state){
            this.alertId=id;
            this.showPopup=true;
            this.remark='';
        },
        //调提交-去处理接口
        clickConfirm(){
            if(this.remark==''){
                vant.Toast('请输入处理结果');
                return;
            }
            if(this.bstap==false){
                return;
            }          
            var that=this;
            var url=this.host+'/app/elderAlert.handleElderAlert';
            var params={
                id:this.alertId,
                remark:this.remark,
                state:'1'
            }
            that.bstap=false;
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='0000'){
                        that.bstap=true;
                        that.showPopup=false;
                        vant.Toast.success('处理成功！');
                        that.pageIndex=1;
                        that.alertList=[];
                        that.loadList();
                        that.loadListProcessed();
					}else{
                        that.bstap=true;
                        that.showPopup=false;
                        vant.Toast.fail('处理失败！');
					}
				},
				error:function(res){
                    that.bstap=true;
                    that.showPopup=false;
                    vant.Toast.fail('处理失败！');
				}
			})
        },
        //待处理
        onDownRefresh() {
            this.pageIndex++;
            this.finished = false // 不写这句会导致你上拉到底过后在下拉刷新将不能触发下拉加载事件
            this.loadList() // 加载数据
        },
        onRefresh() {
            // // 清空列表数据
            this.finished = false;
            if(this.list_length<=0){
                this.refreshing=false;
            }
            // // 重新加载数据
            // // 将 loading 设置为 true，表示处于加载状态
            this.loading = true;
            var that=this;
            this.pageIndex=1;
            setTimeout(function(){
                that.loadList();
            },1000)
        },
        loadList(){
            var that=this;
            var url=this.host+'/app/elderAlert.listElderAlert';
            var params={
                page:this.pageIndex,
                row:10,
                state:'0'
            }
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='200'){
                        that.totalPage=res.records;
                        var dataList=res.data;
                        that.list_length=dataList.length;
                        // console.log(163,dataList)
                        if(that.list_length<1){
                            that.finished_text=''
                        }else{
                            that.finished_text='没有更多了'
                        }
                        if( dataList==null || dataList=='' || dataList.length == 0){
                            // 加载结束
                            that.refreshing=false
                            that.loading = false;
                            that.finished = true;
                            return
                        }
                        if (that.refreshing) {
                            that.alertList = [];
                            that.refreshing = false;
                        }
                         if (params.page === 1) {
                            that.alertList = dataList
                        } else {
                            that.alertList = that.alertList.concat(dataList)
                        }
                        that.loading = false;
                        if (params.page == that.totalPage) {
                            that.finished = true;
                        }
					}else{
                        that.refreshing=false
                        that.loading = false;
                        that.finished_text='';
                        that.finished = true;
                        that.showLoading=false;
                        vant.Toast(res.msg);
					}
				},
				error:function(res){
                    that.refreshing=false
                    that.loading = false;
                    that.finished_text='';
                    that.finished = true;
                    that.showLoading=false;
				}
			})
        },


        //已处理
        onDownRefreshEd() {
            this.pageIndex_ed++
            this.finished_ed = false // 不写这句会导致你上拉到底过后在下拉刷新将不能触发下拉加载事件
            this.loadListProcessed() // 加载数据
        },
        onRefreshEd() {
            // // 清空列表数据
            this.finished_ed = false;
            if(this.list_length_ed<=0){
                this.refreshing_ed=false    ;
            }
            // // 重新加载数据
            // // 将 loading 设置为 true，表示处于加载状态
            this.loading_ed = true;
            var that=this;
            this.pageIndex_ed=1;
            setTimeout(function(){
                that.loadListProcessed();
            },1000)
        },
        loadListProcessed(){
            var that=this;
            var url=this.host+'/app/elderAlert.listElderAlert';
            if(this.pageIndex_ed==0){
                return
             }
            var params={
                page:this.pageIndex_ed,
                row:10,
                state:'1'
            }
            $.ajax({
				url : url,
				type: 'POST',
				dataType:'json',
                contentType: 'text/plain',
                headers:that.requestHeaders,
                data: JSON.stringify(params),
				success : function (res){
					if(res.code=='200'){
                        that.totalPage_ed=res.records;
                        var dataList=res.data;
                        that.list_length_ed=dataList.length;
                        // console.log(163,dataList)
                        if(that.list_length_ed<1){
                            that.finished_text_ed=''
                        }else{
                            that.finished_text_ed='没有更多了'
                        }
                        if( dataList==null || dataList=='' || dataList.length == 0){
                            // 加载结束
                            that.refreshing_ed=false
                            that.loading_ed = false;
                            that.finished_ed = true;
                            return
                        }
                        if (that.refreshing_ed) {
                            that.alertListResult = [];
                            that.refreshing_ed = false;
                        }
                         if (params.page === 1) {
                            that.alertListResult = dataList
                        } else {
                            that.alertListResult = that.alertListResult.concat(dataList)
                        }
                        that.loading_ed = false;
                        if (params.page == that.totalPage_ed) {
                            that.finished_ed = true;
                        }
					}else{
                        that.refreshing_ed=false
                        that.loading_ed = false;
                        that.finished_text_ed='';
                        that.finished_ed = true;
                        that.showLoading_ed=false;
                        vant.Toast(res.msg);
					}
				},
				error:function(res){
                    that.refreshing_ed=false
                    that.loading_ed = false;
                    that.finished_text_ed='';
                    that.finished_ed = true;
                    that.showLoading_ed=false;
				}
			})
        },
        hrefMap(){
            window.location.href='/bigScreen/map.html'
        }
	}
});
